package com.example.day15;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main3Activity extends AppCompatActivity {
    ListView listView;
    List<Map<String,Object>> dataList;
    MyAdapter3 myAdapter;
    Handler myhan = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {      //判断标志位
                case 100:
                    String jsonStr = msg.obj.toString();
                    Log.e("jsonSTR", jsonStr);
                    try {
                        JSONObject json = new JSONObject(jsonStr);
                        JSONArray jsA = json.getJSONArray("data");
                        int length = jsA.length();
                        for (int i = 0; i < length; i++) {
                            Map<String, Object> map = new HashMap<>();
                            JSONObject jsobj = jsA.getJSONObject(i);
                            map.put("title", jsobj.getString("title"));
                            map.put("food_str", jsobj.getString("food_str"));
                            map.put("pic", null);
                            Log.e("jsonSTR"+i, jsobj.getString("title"));
                            dataList.add(map);
                        }
                        myAdapter = new MyAdapter3(Main3Activity.this, dataList);
                        listView.setAdapter(myAdapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);
        listView = findViewById(R.id.list_view);
        dataList=new ArrayList<Map<String,Object>>();
        Message message = Message.obtain();
        message.what=100;
        HashMap<String,String> map =new HashMap<>();
        map.put("stage_id",1+"");
        map.put("limit",50+"");
        map.put("page",1+"");
        new WorkThread(WorkThread.STIRING,myhan,message,"http://www.qubaobei.com/ios/cf/dish_list.php",map).start();


    }
}
